import 'package:mobx/mobx.dart';
import 'package:blurb/common/models/topic.dart';
import 'package:blurb/common/models/comment.dart';
import 'package:blurb/common/services/topic_service.dart';

part 'topic_store.g.dart';

class TopicStore = _TopicStore with _$TopicStore;

abstract class _TopicStore with Store {
  @observable
  List<Topic> topics = [];

  @observable
  List<Topic> hotTopics = [];

  @observable
  bool isLoading = false;

  @action
  Future<void> init() async {
    isLoading = true;
    try {
      await TopicService.initDefaultTopicsAndComments();
      topics = await TopicService.getAllTopics();
      hotTopics = await TopicService.getHotTopics();
    } finally {
      isLoading = false;
    }
  }

  @action
  Future<void> refreshTopics() async {
    isLoading = true;
    try {
      topics = await TopicService.getAllTopics();
      hotTopics = await TopicService.getHotTopics();
    } finally {
      isLoading = false;
    }
  }

  @action
  Future<List<Comment>> getTopicComments(int topicId) async {
    return await TopicService.getTopicComments(topicId);
  }

  @action
  Future<void> addComment(Comment comment) async {
    await TopicService.addCommentToTopic(comment);
    topics = await TopicService.getAllTopics();
    hotTopics = await TopicService.getHotTopics();
  }

  @action
  Future<void> addTopic(Topic topic) async {
    await TopicService.addTopic(topic);
    topics = await TopicService.getAllTopics();
    hotTopics = await TopicService.getHotTopics();
  }
} 